/*
 * Copyright (C) 2010 恒康信息科技有限公司
 * 版权所有
 */
package com.jjmmbb.dolist.ex;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.jjmmbb.anydolist.R;
import com.jjmmbb.anydolist.Util;

/**
 * 通用对话框<br/>
 * 最多支持3个按钮,第2,3个按钮默认隐藏<br/>
 * 对话框创建后自动弹出
 * 
 * @author yizhe
 * @date 2012-6-5
 */
public class HKDialog extends Dialog implements OnClickListener {
	Context context;
	public TextView titleView, msgView;
	public Button bt_1, bt_2, bt_3;

	String title, msg = "";

	boolean is1Bt = false; // 是否只要1个按钮

	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.PROGRESS_VISIBILITY_ON);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hk_dialog);

		titleView = (TextView) findViewById(R.id.HKDialog_title);
		msgView = (TextView) findViewById(R.id.HKDialog_msg);
		bt_1 = (Button) findViewById(R.id.HKDialog_bt1);
		bt_2 = (Button) findViewById(R.id.HKDialog_bt2);
		bt_3 = (Button) findViewById(R.id.HKDialog_bt3);

		bt_1.setOnClickListener(this);
		bt_2.setOnClickListener(this);
		bt_3.setOnClickListener(this);

		if (null == title) {
			title = context.getResources().getString(R.string.dialog_title);
		}
		titleView.setText(title);
		msgView.setText(msg);

		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		if (null != p) {
			// WindowManager windowManager = (WindowManager) getWindow();
			// Display display = windowManager.getDefaultDisplay();
			// WindowManager.LayoutParams lp = getWindow().getAttributes();
			// dialog.getWindow().setAttributes(lp);
			p.width = Util.dip2px(context, 280);
			// 设置宽度
			// p.width = (int)(getap.c.screenWidth * 0.8);
		}

		setButtonText();
		if (is1Bt) {
			bt_2.setVisibility(View.GONE);
		}
	}

	/**
	 * 创建对话框,使用默认标题 R.string.dialog_defaultTitle
	 * 
	 * @param context
	 * @param msg
	 *            消息
	 */
	public HKDialog(Context context) {
		super(context);
		this.context = context;
	}

	/**
	 * 创建对话框,使用默认标题 R.string.dialog_defaultTitle
	 * 
	 * @param context
	 * @param msg
	 *            消息
	 */
	public HKDialog(Context context, String msg) {
		super(context);
		this.context = context;
		this.msg = msg;
	}

	/**
	 * 创建对话框
	 * 
	 * @param context
	 * @param title
	 *            标题
	 * @param msg
	 *            消息
	 */
	public HKDialog(Context context, String title, String msg) {
		super(context);
		this.context = context;
		this.title = title;
		this.msg = msg;
	}

	/**
	 * 设置对话框高度
	 * 
	 * @param height
	 */
	public void setHeight(int height) {
		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		if (null != p) {
			p.height = height;
		}
	}

	/**
	 * 设置按钮标题,将在onCreate方法中调用到
	 */
	protected void setButtonText() {

	}

	/**
	 * 设置按钮是否可用
	 * 
	 * @param index
	 *            按钮序号: 1,2,3
	 * @param need
	 *            是否可用
	 */
	public void setButtonVisible(int index, boolean need) {
		switch (index) {
		case 1:
			if (need) {
				bt_1.setVisibility(View.VISIBLE);
			} else {
				bt_1.setVisibility(View.GONE);
			}
			break;
		case 2:
			if (need) {
				bt_2.setVisibility(View.VISIBLE);
			} else {
				bt_2.setVisibility(View.GONE);
			}
			break;
		case 3:
			if (need) {
				bt_3.setVisibility(View.VISIBLE);
			} else {
				bt_3.setVisibility(View.GONE);
			}
			break;
		}

	}

	/**
	 * 设置按钮的文本
	 * 
	 * @param index
	 *            按钮序号
	 * @param text
	 *            按钮文本,为null或者""时不修改
	 */
	public void setButtonText(int index, String text) {
		if (Util.isNull(text)) {
			return;
		}
		switch (index) {
		case 1:
			bt_1.setText(text);
			break;
		case 2:
			bt_2.setText(text);
			break;
		case 3:
			bt_3.setText(text);
			break;
		}
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	// --------------------单击事件,用到请重写-------------------//
	protected void bt1Click() {

		this.dismiss();// 不会触发oncancel事件
	}

	protected void bt2Click() {

		this.dismiss();
	}

	protected void bt3Click() {

		this.dismiss();
	}

	// --------------------------on-----------------------
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.HKDialog_bt1:
			bt1Click();
			break;
		case R.id.HKDialog_bt2:
			bt2Click();
			break;
		case R.id.HKDialog_bt3:
			bt3Click();
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按下键盘上返回按钮
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.dismiss();
		}
		return true;
	}
}
